package com.zjt.service.user;

import com.zjt.dao.user.IRecordDao;
import com.zjt.dao.user.IRecordDaoExpand;
import com.zjt.entity.Record;
import com.zjt.utils.DateUtil;
import com.zjt.web.api.vo.base.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * 物码中心数据采集的DAO interface;
 * 
 * @author xueyanbeijing
 */
@Component
public class RecordService implements IRecordService {
    private static Logger logger = LoggerFactory.getLogger(RecordService.class);
    private IRecordDao recordDao ;
    private IRecordDaoExpand recordDaoExpand;

    @Autowired
    public void setRecordDaoExpand(IRecordDaoExpand recordDaoExpand) {
        this.recordDaoExpand = recordDaoExpand;
    }

    @Autowired
    public void setRecordDao(IRecordDao recordDao) {
        this.recordDao = recordDao;
    }

    @Override
    public List<Record> getRecordListByCodeType(long userId, String codeType, PageVO pageVO) {
        List<Record> list = null;
        if (userId>0)
        {
             list = this.recordDao.findByUserId(userId);
        }else this.recordDao.findAll();
        return list;  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public List<Record> getRecordListByProductName(long userId, String productName) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Record saveRecord(long userId, String codeType, String codeValue, Integer goodsReal) {
        Record record = new Record();
        try
        {
            record.setCodeType(codeType);
            record.setCodeValue(codeValue);
            record.setGoodsReal(goodsReal);
            record.setUserId(userId);
            record.setCreateDate(DateUtil.getCurrentDate());
            record.setUpdateDate(DateUtil.getCurrentDate());
            record.setEnableFlag("Y");
            this.recordDao.save(record);
            return record;
        }catch (Exception e){
            return null;
        }
         //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Record> getRecordListByUserId(long userId, PageVO pageVO) {
        List<Record> list = null;
        Page<Record> pageList = null;
//        Sort sort = new Sort(new Order(Direction.DESC,"updateDate"));  //排序设置
//        Direction direction =  Sort.Direction.DESC;
//        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize(),direction,"updateDate");
        //TODO  如果Userid  大于0  说明是取用户自已的历史记录
        //反之，取所有库中的最新10条
        try
        {
            if (userId>0)
            {
                pageVO.setTotalCount(this.recordDao.countByQueryUserId(userId));
                list = this.recordDaoExpand.getRecordListByUserId(userId,pageVO.getStartRow(),pageVO.getPageSize());

            }
            else
                list = this.recordDaoExpand.getRecordListByUserId(0,0,10);
            //TODO 构造扫描记录返回对像
        }catch (Exception e){
            logger.error("分页取扫描记录异常 ="+e.toString());
            e.printStackTrace();
        }
        return list;
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Record getRecordById(long recordId) {
        return this.recordDao.findOne(recordId);  //To change body of implemented methods use File | Settings | File Templates.
    }
}
